﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using BEPUphysics.Entities;

namespace Asplode.Renderer
{
    public class AsplodeRendererEventHandler
    {

        public AsplodeRenderer Renderer { get; set; }

        public AsplodeRendererEventHandler(AsplodeRenderer renderer)
        {
            Renderer = renderer;
        }

        public void subscribe(EventDispatcher EventDispatcher)
        {
            EventDispatcher.BoxSpawn += new EventDispatcher.BoxSpawnHandler(EventDispatcher_BoxSpawn);
            EventDispatcher.BoxShot += new EventDispatcher.BoxShotHandler(EventDispatcher_BoxShot);
            EventDispatcher.CursorSpawn += new EventDispatcher.CursorSpawnHandler(EventDispatcher_CursorSpawn);
            EventDispatcher.Explosion += new EventDispatcher.ExplosionHandler(EventDispatcher_Explosion);
            EventDispatcher.HumanSpawn += new EventDispatcher.HumanSpawnHandler(EventDispatcher_HumanSpawn);
            EventDispatcher.MortarShot += new EventDispatcher.MortarShotHandler(EventDispatcher_MortarShot);
            EventDispatcher.PlayerAim += new EventDispatcher.PlayerAimHandler(EventDispatcher_PlayerAim);
            EventDispatcher.RemoveEntity += new EventDispatcher.RemoveEntityHandler(EventDispatcher_RemoveEntity);
            EventDispatcher.ZombieSpawn += new EventDispatcher.ZombieSpawnHandler(EventDispatcher_ZombieSpawn);
        }

        void EventDispatcher_BoxShot(Entity Box, Vector3 initialForce)
        {
            EventDispatcher_BoxSpawn(Box as Box, initialForce);
        }

        void EventDispatcher_CursorSpawn(Entity Player, Entity Cursor)
        {
            Model model = Renderer.game.Content.Load<Model>("Models\\sphere");
            RenderPair rp = new RenderPair(Cursor, model);
            rp.transform = Matrix.CreateScale(0.75f, 0.75f, 0.75f)
                           *Matrix.CreateTranslation(0f,0.5f,0f);
            Renderer.models.Add(rp);   
        }

        void EventDispatcher_PlayerAim(Entity entity, Vector3 location)
        {

        }

        void EventDispatcher_BoxSpawn(Box Box, Vector3 initialForce)
        {
            Model model = Renderer.game.Content.Load<Model>("Models\\cube");
            RenderPair rp = new RenderPair(Box, model);
            rp.transform = Matrix.CreateScale(Box.width, Box.height, Box.length);
            Renderer.models.Add(rp);
        }

        void EventDispatcher_Explosion(Vector3 position, float magnitude, float maxRange)
        {
            Model model = Renderer.game.Content.Load<Model>("Models\\sphere");
            Entity sphere = new Sphere(position, maxRange);
            Renderer.explosions.Enqueue(new RendererExplosion(model, position, magnitude, maxRange));
        }

        void EventDispatcher_HumanSpawn(Entity entity)
        {
            Model model = Renderer.game.Content.Load<Model>("Models\\character");
            RenderPair rp = new RenderPair(entity, model);
            rp.transform = Matrix.CreateScale(2, 2, 2);
            Renderer.models.Add(rp);
        }

        void EventDispatcher_MortarShot(Entity entity, Vector3 initialForce)
        {
            Model model = Renderer.game.Content.Load<Model>("Models\\sphere");
            RenderPair rp = new RenderPair(entity, model);
            rp.transform = Matrix.CreateScale(0.75f, 0.75f, 0.75f);
            Renderer.models.Add(rp);
        }

        void EventDispatcher_RemoveEntity(Entity entity)
        {
            foreach (RenderPair rp in Renderer.models)
            {
                if (rp.Entity == entity)
                {
                    Renderer.models.Remove(rp);
                    return;
                }
            }
        }

        void EventDispatcher_ZombieSpawn(Entity entity)
        {
            Model model = Renderer.game.Content.Load<Model>("Models\\bunny");
            RenderPair rp = new RenderPair(entity, model);
            rp.transform = Matrix.CreateRotationY(MathHelper.ToRadians(90)) * Matrix.CreateScale(2);
            Renderer.models.Add(rp);
        }
    }
}
